/**
 * Copyright 2015 Software innovation and R & D center. All rights reserved.
 * File Name: SysUserController.java
 * Encoding UTF-8
 * Version: 0.0.1
 * History:	2015年1月20日
 */
package com.sirdc.modules.sys.web;

import com.sirdc.modules.core.exceptions.ServiceException;
import com.sirdc.modules.core.exceptions.UserExistException;
import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.sys.databean.SysUserDataBean;
import com.sirdc.modules.sys.entity.SysDept;
import com.sirdc.modules.sys.entity.SysLogin;
import com.sirdc.modules.sys.entity.SysUser;
import com.sirdc.modules.sys.filter.SysUserFilter;
import com.sirdc.modules.sys.service.SysDeptService;
import com.sirdc.modules.sys.service.SysUserService;
import com.sirdc.modules.sys.util.UserUtils;
import com.sirdc.modules.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统用户表维护
 * @author: huiyang.yu
 * @version Revision: 0.0.1
 * @Date: 2015年1月20日
 */
@Controller
@RequestMapping(value = "/modules/sys/userzoom")
public class SysUserZoomController extends JsonBaseController {
	
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysDeptService sysDeptService;
	
	@Override
	protected String getView(String view) {
		return "/modules/sys/userzoom/" + view;
	}

	/**
	 * 初始化用户表列表界面
	 * 
	 * @return url
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String initPage() {
		return getView("query");
	}

	/**
	 * 输出json用户表
	 * 
	 * @param filter
	 * @return json
	 */
	@ResponseBody
	@RequestMapping(method = RequestMethod.POST)
	public JqGrid query(@ModelAttribute SysUserFilter filter) {
		String userId = UserUtils.getUserId();
//		filter.setUserId(userId);
		ArrayList<String> inIds = new ArrayList<>();
		inIds.add(userId);
		filter.setInIds(inIds);
		List<SysUserDataBean> sysUserDataBeans = sysUserService.queryOnDatabean(filter);
		return coverJqGrid(filter, sysUserDataBeans);
	}
	
	/**
	 * 跳转至filter界面
	 * @param filter
	 * @return
	 */
	@RequestMapping("/filter")
	public String filter(@ModelAttribute SysUserFilter filter) {
		return getView("filter");
	}
	





	/**
	 * 初始化更新用户表界面
	 * 
	 * @param model
	 * @param id
	 * @return url
	 */
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String showEditView(Model model, @PathVariable String id) {
		//根据自己的过滤规则过滤部门，将来可以考虑是否要修改成下拉树

		List<SysDept> depts = sysDeptService.query();
		SysUserDataBean datebean = sysUserService.getUserDataBeanById(id);
		model.addAttribute("datebean", datebean);
		model.addAttribute("depts", depts);
		return getView("form");
	}
	
	/**
	 * 更新用户表操作
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public Message update(@ModelAttribute SysUserDataBean datebean) {
		sysUserService.updateUserLogin(datebean);
		return coverMessage("200");
	}

	/**
	 * 查询用户表并跳转至详情界面
	 * @param id
	 * @param model
	 * @return url
	 */
	@RequiresPermissions("modules:sys:user:detail")
	@RequestMapping(value = "/detail/{id}")
	public String detail(@PathVariable String id, Model model) {
		SysUser sysUser = sysUserService.getById(id);

		if (sysUser == null) {
			throw new ServiceException("message");
		}

		model.addAttribute("sysUser", sysUser);
		return getView("detail");
	}
	

	



	@ResponseBody
	@RequestMapping(value = "/changePass")
	public Message changePass(@ModelAttribute SysUserDataBean databean) {
		if (StringUtils.isEmpty(databean.getPassword()) || StringUtils.isEmpty(databean.getOldPassword())) {
			return coverMessage("500", "请输入完整的信息");
		}
		String userId = UserUtils.getUserId();
		SysLogin login = sysUserService.getSysLoginById(userId);
		String encryptPass = sysUserService.getEncryPass(databean.getOldPassword(), login.getSaltKey());
		if(login.getPassword().equals(encryptPass)) {
			databean.setUserId(userId);
			sysUserService.updatePassword(databean);
			return coverMessage("200");
		}
		return coverMessage("500", "对不起，您当前输入的旧密码有误");
	}
	

}